package com.tamir.template.util;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : tanggh
 * @description :〈描述〉
 * @date : 2016/10/31
 */
public class StringUtil
{
    public static final char UNDERLINE = '_';

    //数字表达式
    public static final Pattern NUMERIC_PATTERN = Pattern.compile("^-?[0-9]+(.[0-9]+)?$");

    public static boolean isEmpty(Object str)
    {
        return str == null || "".equals(str);
    }

    public static boolean isNotEmpty(Object str)
    {
        return !isEmpty(str);
    }

    public static String getGetMethodNameWithAttribute(String attribute)
    {
        return "get" + changeFirstCharacterCase(attribute, true);
    }

    public static String upperFirstCharacter(String str)
    {
        return changeFirstCharacterCase(str, true);
    }

    public static String lowerFistCharacter(String str)
    {
        return changeFirstCharacterCase(str, false);
    }

    private static String changeFirstCharacterCase(String str, boolean capitalize)
    {
        if (str != null && str.length() != 0)
        {
            StringBuilder sb = new StringBuilder(str.length());
            if (capitalize)
            {
                sb.append(Character.toUpperCase(str.charAt(0)));
            }
            else
            {
                sb.append(Character.toLowerCase(str.charAt(0)));
            }

            sb.append(str.substring(1));
            return sb.toString();
        }
        else
        {
            return str;
        }
    }

    public static String camelToUnderline(String param)
    {
        if (param == null || "".equals(param.trim()))
        {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
        {
            char c = param.charAt(i);
            if (Character.isUpperCase(c))
            {
                sb.append(UNDERLINE);
                sb.append(Character.toLowerCase(c));
            }
            else
            {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel(String param)
    {
        if (param == null || "".equals(param.trim()))
        {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
        {
            char c = param.charAt(i);
            if (c == UNDERLINE)
            {
                if (++i < len)
                {
                    sb.append(Character.toUpperCase(param.charAt(i)));
                }
            }
            else
            {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    public static String underlineToCamel2(String param)
    {
        if (param == null || "".equals(param.trim()))
        {
            return "";
        }
        StringBuilder sb = new StringBuilder(param);
        Matcher mc = Pattern.compile("_").matcher(param);
        int i = 0;
        while (mc.find())
        {
            int position = mc.end() - (i++);
            //String.valueOf(Character.toUpperCase(sb.charAt(position)));
            sb.replace(position - 1, position + 1, sb.substring(position, position + 1).toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 字符转布尔类型
     * @param express
     * @return
     */
    public static Boolean string2Boolean(String express){
        Boolean flag = false;
        if(StringUtil.isNotEmpty(express)){
            if(isNumeric(express)){
                BigDecimal bd = new BigDecimal(express);
                if(bd.compareTo(BigDecimal.ZERO) > 0){
                    flag = true;
                }
            }else{
                flag = new Boolean(express);
            }
        }
        return flag;
    }

    /**
     * 判断是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        boolean numeric = false;
        if(isNotEmpty(str)){
            Matcher matcher = NUMERIC_PATTERN.matcher(str);
            numeric = matcher.matches();
        }
        return numeric;
    }

    public static boolean isBoolean(String str){
        if(StringUtil.isNotEmpty(str)){
            if(str.equals("true")||str.equals("false")){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取string中匹配字符左边的字符串
     * @param string
     * @param matcher
     * @return
     */
    public static String subLeftString(String string, Matcher matcher){
        if(StringUtil.isNotEmpty(string) && matcher != null){
            if(matcher.find()){
                return string.substring(0, matcher.start());
            }else{
                return "";
            }
        }
        return string;
    }

    /**
     * 获取匹配的字符串
     * @param string
     * @param matcher
     * @return
     */
    public static String getMatchString(String string, Matcher matcher){
        if(StringUtil.isNotEmpty(string) && matcher != null){
            if(matcher.find()){
                return string.substring(matcher.start(), matcher.end());
            }else{
                return null;
            }
        }
        return string;
    }
    /**
     * 获取string中匹配字符右边的字符串
     * @param string
     * @param matcher
     * @return
     */
    public static String subRightString(String string, Matcher matcher){
        if(StringUtil.isNotEmpty(string) && matcher != null){
            if(matcher.find()){
                return string.substring(matcher.end(), string.length());
            }else{
                return "";
            }
        }
        return string;
    }


    public static void main(String[] args) throws Exception
    {
        System.out.println(StringUtil.isNumeric("-1"));
        System.out.println(StringUtil.isNumeric("-1.01"));
        System.out.println(StringUtil.isNumeric("2.5"));
        System.out.println(StringUtil.isNumeric("2"));

        System.out.println("-------");
        System.out.println(string2Boolean("111"));
        System.out.println(string2Boolean("aaa"));
        System.out.println(string2Boolean("false"));
        System.out.println(string2Boolean("true"));
        System.out.println(string2Boolean("0"));
        System.out.println(string2Boolean("-0.1"));
        String elseStr = "cccc<else>aaa";
        Pattern elsePattern = Pattern.compile("<else>");
        Matcher matcher = elsePattern.matcher(elseStr);
        System.out.println(StringUtil.subLeftString(elseStr, matcher));
        matcher.reset();
        System.out.println(StringUtil.subRightString(elseStr, matcher));
        matcher.reset();
        System.out.println(elseStr.substring(matcher.start(), matcher.end()));
    }
}
